package app.im;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.menhoo.toollibrary.android.log.Log;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;

import app.AddressBookDeptSelector;
import application.HttpUrlConstant;
import application.UIApplication;
import custom.PinnedSectionListView;
import menhoo.com.i8officelibrary.R;
import model.EmployeeItemDBModel;

/**
 * Created by ThinkPad on 2017/5/15.
 */

public class IMAddChatListAdapter  extends BaseAdapter implements PinnedSectionListView.PinnedSectionListAdapter {

    private final List<EmployeeItemDBModel> data = new ArrayList<EmployeeItemDBModel>();
    private LayoutInflater inflate;
    private BitmapUtils bitmapUtils;
    public IMAddChatListAdapter(Context context, List<EmployeeItemDBModel> list) {

        inflate = LayoutInflater.from(context);
        bitmapUtils = new BitmapUtils(context, UIApplication.getAppCachePath(context));

        initGetData(list);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public EmployeeItemDBModel getItem(int position) {
        Log.e("getItem", "position:"+position);
        if(position >= data.size()){
            return null;
        }
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        Log.e("getView", "---------------------");
        Log.e("getView1", "position:"+position);
        EmployeeItemDBModel item = getItem(position);
        Log.e("getView2", "position:"+position);

        if(item != null) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                if (item.type == EmployeeItemDBModel.ITEM) {
                    convertView = inflate.inflate(R.layout.adapter_item_address_book_dept, null);
                    viewHolder.img = (ImageView) convertView.findViewById(R.id.imageView1);
                } else {
                    convertView = inflate.inflate(R.layout.adapter_item_address_book_only_text, null);
                }
                viewHolder.content = (TextView) convertView.findViewById(R.id.textView1);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            if (item.type == EmployeeItemDBModel.ITEM) {

                if (item.EmplID != null && !item.EmplID.equalsIgnoreCase("")) {
                    Map<String, String> parm = new HashMap<String, String>();
                    parm.put("emplid", item.EmplID);
                    bitmapUtils.display(viewHolder.img, HttpUrlConstant.getWebSiteFullUrl(HttpUrlConstant.doShowPhoto,
                            parm));
                } else {
                    viewHolder.img.setImageResource(R.mipmap.group_chat);
                }

            }

            viewHolder.content.setText(item.Name);

        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {

        Log.e("getItemViewType", "---------------------");
        Log.e("getItemViewType1", "position:"+position);
        EmployeeItemDBModel item = getItem(position);
        Log.e("getItemViewType2", "position:"+position);
        if( item ==  null ){
            return 0;
        }else {
            return getItem(position).type;
        }
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
        return viewType == EmployeeItemDBModel.SECTION;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public void changeData(List<EmployeeItemDBModel> addData) {
        initGetData(addData);
        notifyDataSetChanged();
    }

    private void initGetData(List<EmployeeItemDBModel> list){

        int sectionPosition = -1, listPosition = 0;
        for (EmployeeItemDBModel section : list) {
            if (section.type == EmployeeItemDBModel.SECTION) {
                sectionPosition++;
            }
            section.sectionPosition = sectionPosition;
            section.listPosition = listPosition++;

        }
        data.clear();
        data.addAll(list);
    }

    class ViewHolder {
        TextView content;
        ImageView img;
    }
}